package zero.tech.games.pdk.util;

import java.util.List;
import java.util.Map;

import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.RoomStatus;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.games.pdk.data.PdkConfig;
import zero.tech.games.pdk.data.PdkGameBase;
import zero.tech.games.pdk.data.PdkResGameBase;
import zero.tech.games.pdk.data.PdkResReconnect;
import zero.tech.games.pdk.data.PdkRoomCache;
import zero.tech.games.pdk.data.PdkTimer;
import zero.tech.games.pdk.data.entity.DispatchData;
import zero.tech.games.pdk.data.entity.PlayerBigCal;
import zero.tech.games.pdk.data.entity.PlayerData;
import zero.tech.games.pdk.data.notify.NotifyChangePlayer;
import zero.tech.games.pdk.data.notify.NotifyDispatchCard;
import zero.tech.games.pdk.data.notify.NotifyGameStart;
import zero.tech.games.pdk.data.res.ResPdkHandCards;
import zero.tech.games.pdk.data.res.ResSetcards;
import zero.tech.games.pdk.data.res.ResSetcardsLeft;
import zero.tech.wsnet.controller.LogicController;
public class PackageUtil {
	public static NotifyGameStart packageNotifyGameStart(Room room, LogicController logicController) {
		int uid=logicController.getId();
		PdkConfig pdkConfig=(PdkConfig) room.getGameConfig();
		PdkRoomCache pdkRoomCache = (PdkRoomCache) room.getRoomCache();
		PdkGameBase pdkGameBase = (PdkGameBase) room.getGameBase();
		NotifyGameStart notifyGameStart=new NotifyGameStart();
		Map<Integer,PlayerData> map=pdkGameBase.getPlayers();
		PlayerData playerData=map.get(uid);
		notifyGameStart.getHands().addAll(playerData.getHands().values());
		notifyGameStart.setDispatch_uid(pdkGameBase.getDispatch_uid());
		notifyGameStart.setLeft_timer(pdkConfig.getSpeed());
		notifyGameStart.getBirds().putAll(pdkRoomCache.getBirds());
		return notifyGameStart;
	}

	public static NotifyChangePlayer packageNotifyChangePlayer(Room room, int cur_uid) {
		NotifyChangePlayer notifyChangePlayer = new NotifyChangePlayer();
		PdkConfig pdkConfig = (PdkConfig) room.getGameConfig();
		PdkGameBase pdkGameBase=(PdkGameBase) room.getGameBase();
		int next_uid=pdkGameBase.getDispatch_uid();
		int left_timer=pdkConfig.getSpeed();
		notifyChangePlayer.setNext_uid(next_uid);
		notifyChangePlayer.setLeft_timer(left_timer);
		return notifyChangePlayer;
	}

	public static ResReconnet packageResPdkReconnect(Room room, LogicController online) {
		ResReconnet resReconnet=new PdkResReconnect();
		PckUtil.packageResReconnect(room,online,resReconnet);
		int room_status=room.getRoom_status();
		if(room_status==RoomStatus.GAMING) {
			resReconnet.setRes_gamebase(packageResPdkGameBase(room,online));
		}
		return resReconnet;
	}

	private static PdkResGameBase packageResPdkGameBase(Room room, LogicController online) {
		int uid=online.getId();
		long now=System.currentTimeMillis();
		PdkRoomCache pdkRoomCache = (PdkRoomCache) room.getRoomCache();
		PdkResGameBase resPdkGameBase = new PdkResGameBase();
		PdkGameBase pdkGameBase = (PdkGameBase) room.getGameBase();
		PdkTimer pdkTimer=(PdkTimer) room.getTimer();
		PdkConfig pdkConfig=(PdkConfig) room.getGameConfig();
		resPdkGameBase.setDispatch_uid(pdkGameBase.getDispatch_uid());
		Map<Integer,PlayerData> map=pdkGameBase.getPlayers();
		for(PlayerData pd:map.values()) {
			resPdkGameBase.getLeft_map().put(pd.getUid(),pd.getHands().size());
		}
		PlayerData playerData=map.get(uid);
		resPdkGameBase.getHands().addAll(playerData.getHands().values());
		resPdkGameBase.getLast_dispatchs().putAll(pdkGameBase.getLast_dispatchs());
		int left_dispatch_timer=(int) (pdkConfig.getSpeed()-(now-pdkTimer.lastDispatchTime)/1000);
		resPdkGameBase.setLeft_dispatch_timer(left_dispatch_timer);
		resPdkGameBase.getAutodeposit().addAll(pdkGameBase.getAuto_deposit().keySet());
		resPdkGameBase.getBirds().putAll(pdkRoomCache.getBirds());
		resPdkGameBase.setPre(pdkGameBase.getPre());
		return resPdkGameBase;
	}

	public static NotifyDispatchCard packageNotifyDispatchCard(Room room, DispatchData dispatchData) {
		NotifyDispatchCard notifyDispatchCard = new NotifyDispatchCard();
		notifyDispatchCard.setDispatchData(dispatchData);
		return notifyDispatchCard;
	}


	public static PlayerBigCal packagePlayerBigCal(LogicController logicController) {
		PlayerBigCal playerBigCal = new PlayerBigCal();
		playerBigCal.setSimpleUser(logicController.getSimpleUser());
		return playerBigCal;
	}
	
	public static ResPdkHandCards packageResPdkHandCards(PlayerData playerData) {
		ResPdkHandCards resPdkHandCards= new ResPdkHandCards();
		resPdkHandCards.getCards().addAll(playerData.getHands().values());
		return resPdkHandCards;
	}

	public static ResSetcardsLeft packageResSetcardsLeft(List<Integer> cardIds) {
		ResSetcardsLeft resSetcardsLeft = new ResSetcardsLeft();
		resSetcardsLeft.getCardIds().addAll(cardIds);
		return resSetcardsLeft;
	}

	public static ResSetcards packageResSetcards(List<Integer> cardIds) {
		ResSetcards resSetcards = new ResSetcards();
		resSetcards.getCardIds().addAll(cardIds);
		return resSetcards;
	}

}
